package cn.nexd.map.rendering.core.componet;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.MotionEvent;

import java.util.Map;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.data.Feature;
import cn.nexd.map.rendering.core.componet.data.FloorsFeature;
import cn.nexd.map.utils.BitmapUtils;


/**
 * Created by codingfish on 15/8/4.
 */
public class MapTextView extends SVGMapBaseOverlay {

    private MapMainView mapMainView;
    private FloorsFeature floorsFeature;
    private Context context;

    private TextPaint textPaint;

    public MapTextView(MapMainView mapMainView, FloorsFeature floorsFeature, Context context) {
        this.mapMainView = mapMainView;
        this.floorsFeature = floorsFeature;
        this.context = context;

        showLevel = LOCATION_LEVEL;
        textPaint = new TextPaint();
        textPaint.setTextSize(BitmapUtils.TEXTSIZE);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setFakeBoldText(true);
        textPaint.setColor(Color.BLACK);
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onTap(MotionEvent event) {

    }

    @Override
    public void draw(Canvas canvas, Matrix matrix, float currentZoom, float currentRotateDegrees) {
        canvas.save();
//        matrix.setScale(0, 0);
//        canvas.setMatrix(matrix);


        if (currentZoom > mapMainView.getMaxZoomValue() - BitmapUtils.SHOWTEXTSIZE) {
            for (int i = 0; i < floorsFeature.getFeatures().size(); i++) {
                Map<Long, Feature> map = floorsFeature.getFeatures().get(i);
                for (Map.Entry<Long, Feature> entry : map.entrySet()) {
                    Feature feature = entry.getValue();
                    if (!TextUtils.equals("", feature.getName())) {

                        canvas.save();
                        Rect rect = new Rect();
                        rect.set((int) feature.getMinX(), (int) feature.getMinY(), (int) feature.getMaxX(), (int) feature.getMaxY());
//                    canvas.setMatrix(matrix);
                        Paint rectPaint = new Paint();
                        rectPaint.setAlpha(10);

                        canvas.restore();


                        canvas.save();
                        float gol[] = {(float) feature.getCenter_x(), (float) feature.getCenter_y()};
                        matrix.mapPoints(gol);
                        canvas.drawText(feature.getName(), gol[0], gol[1], textPaint);
                        canvas.restore();
                    }


                    /////////


//                String spaceTypeString = entry.getValue().getSpaceTypeString();
//
//                float center_x = feature.getCenter_x();
//                float center_y = feature.getCenter_y();
//                float gol[] = {center_x, center_y};
//                matrix.mapPoints(gol);
//                Bitmap bitmap = null;
//                canvas.save();
//                if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "01")) {
//                    // 会议室
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_meetingroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_meetingroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "02")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_toilet.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_toilet.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "04")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_elevator.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_elevator.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "05")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_stairs.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_stairs.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "07")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_parking.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_parking.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "17")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_diningroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_diningroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                }
//                canvas.restore();

                }
            }
        }


        for (int i = 0; i < floorsFeature.getFeatures().size(); i++) {
            Map<Long, Feature> map = floorsFeature.getFeatures().get(i);
            for (Map.Entry<Long, Feature> entry : map.entrySet()) {
                Feature feature = entry.getValue();
//                if (!TextUtils.equals("", feature.getName())) {
//
//                    canvas.save();
//                    Rect rect = new Rect();
//                    rect.set((int) feature.getMinX(), (int) feature.getMinY(), (int) feature.getMaxX(), (int) feature.getMaxY());
////                    canvas.setMatrix(matrix);
//                    Paint rectPaint = new Paint();
//                    rectPaint.setAlpha(10);
//
//                    canvas.restore();
//
//
//                    canvas.save();
//                    float gol[] = {(float) feature.getCenter_x(), (float) feature.getCenter_y()};
//                    matrix.mapPoints(gol);
//                    canvas.drawText(feature.getName(), gol[0], gol[1], textPaint);
//                    canvas.restore();
//                }


                /////////


                String spaceTypeString = entry.getValue().getSpaceTypeString();

                float center_x = feature.getCenter_x();
                float center_y = feature.getCenter_y();
                float gol[] = {center_x, center_y};
                matrix.mapPoints(gol);
                Bitmap bitmap = null;
                canvas.save();

//                if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "01")) {
//                    // 会议室
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_meetingroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_meetingroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//
//                } else


                if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "02")) {
//                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_toilet.png");
                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_toilet.png");
                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "04")) {
//                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_elevator.png");
                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_elevator.png");
                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "05")) {
//                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_stairs.png");
                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_stairs.png");
                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
                }

//                else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "07")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_parking.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_parking.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "17")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_diningroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_diningroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                }
                canvas.restore();

            }
        }


        canvas.restore();
    }
}
